package co.yixiang.yshop.module.order.dal.redis.order;

import co.yixiang.yshop.framework.common.util.json.JsonUtils;
import co.yixiang.yshop.module.order.controller.admin.storeorderrule.vo.StoreOrderRuleVO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

import static co.yixiang.yshop.module.order.dal.redis.RedisKeyConstants.YSHOP_ORDER_RULE_CACHE_KEY;

/**
 * 订单规则的 RedisDAO
 *
 * @author yshop
 */
@Repository
public class OrderRuleRedisDAO {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public StoreOrderRuleVO get() {
        String redisKey = YSHOP_ORDER_RULE_CACHE_KEY.getKeyTemplate();
        return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), StoreOrderRuleVO.class);
    }

    public void set(StoreOrderRuleVO vo) {
        String redisKey = YSHOP_ORDER_RULE_CACHE_KEY.getKeyTemplate();
        stringRedisTemplate.opsForValue().set(redisKey, JsonUtils.toJsonString(vo));
    }

    public void delete() {
        String redisKey = YSHOP_ORDER_RULE_CACHE_KEY.getKeyTemplate();
        stringRedisTemplate.delete(redisKey);
    }


    private static String formatKey(String key) {
        return String.format(YSHOP_ORDER_RULE_CACHE_KEY.getKeyTemplate(), key);
    }
}
